var pageParams;

//返回值为需不需要关闭窗口
function yesOperator() {
    var retValue = addData();
    return retValue;
}
function addLoad(){
    $("#b_mhId").val(pageParams.pId);
    //$("#b_mhCode").val(pageParams.pCode);

}
function updateLoad() {
    var params = $$.getQueryString();
    $$.ajax({
        url: "/rest/IotMhDataBusiness/query",
        data: {
            id: params.rowId
        },
        success: function (data) {
            $("#b_id").val(data.id);
            $("#b_mhId").val(data.mhId);
            $("#b_dataTable").val(data.dataTable);
            $("#b_dataCode").val(data.dataCode);
            $("#b_dataName").val(data.dataName);
            $("#b_dataColumn").val(data.dataColumn);
            $("#b_dataExpr").val(data.dataExpr);
            $("#b_dataDexpr").val(data.dataDexpr);
            $("#b_dataScale").val(data.dataScale);
            $("#b_status").val(data.status);
            $("#b_rowSort").val(data.rowSort);
            $("#b_remarks").val(data.remarks);

            var status = data.status;
            $("input[name^='b_status_']").each(function () {
                if ($(this).val() == status) {
                    this.checked = 'checked';
                }
            });

            $("#b_dataCodeOld").val(data.dataCode);
        }
    });
}

function validForm() {
    return $('#theform').valid();
}

function addData() {
    var param = {};
    var validFormRet;
    $$.form({
        async: false,
        able: function () {
            validFormRet = false;
        },
        checkForm: validForm,
        success: function () {
            validFormRet = true;
        }
    });
    return validFormRet;
}

function forwardCalcValid() {
    $("#theform").validate({
        rules: {
            b_mhId: {

                required: true,

                minlength: 1,
                number: false
            },
            b_dataTable: {

                required: true,

                minlength: 1,
                number: false
            },
            b_dataCode: {
                required: true,
                minlength: 1,
                isCode: true
            },
            b_dataName: {

                required: true,

                minlength: 1,
                number: false
            },
            b_dataColumn: {

                required: true,

                minlength: 1,
                number: false
            },
            b_dataExpr: {

                required: true,
                isRptMhDataFormula:true,
                minlength: 1,
                number: false
            },
            b_dataDexpr: {
                isRptDatetimeFormula:true,
                required: true,
                minlength: 1,
                number: false
            },
            b_dataScale: {

                required: true,

                minlength: 1,
                number: false
            },
            b_status: {

                required: true,

                minlength: 1,
                number: false
            },
            b_rowSort: {
                required: true,
                minlength: 1,
                isPositiveInteger: true
            }
        },
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        errorElement: "span",
        errorClass: "errortips",
        success: 'valid',
        messages: {
            b_mhId: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_dataTable: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_dataCode: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_dataName: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_dataColumn: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_dataExpr: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_dataDexpr: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_dataScale: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_status: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_rowSort: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },

            b_rowSort: {
                required: " (必填字段)",
                minlength: " (不能少于1位编号)",
                number: "请输入数字",
            }
        },
    });
}

$(function () {
    pageParams = $$.getQueryString();
    forwardCalcValid();
    $("input[name^='b_status']").click(function () {
        $("#b_status").val($(this).val());
    });
});

jQuery.validator.addMethod("isCode", function(value, element) {




    //校验编码是否存在
    var dataCodeOld=$("#b_dataCodeOld").val();
    var dataCode=$("#b_dataCode").val();
    if(dataCodeOld==dataCode){
        return true;
    }else{
        if(!/^[A-Z]{3}[0-9]{4}$/.test($("#b_dataCode").val())){
            $(element).data('error-msg','请输入3位大写字母＋4位数字');
            return false;
        }

            var args={};
            args.c_mhId_1=$("#b_mhId").val();
            args.c_dataCode_1=$("#b_dataCode").val();
            args.page=0;
            args.pageSize=5;
            args.isPage=1;
            var exists;
            $$.ajax({
                url: "/bootstrap/basegrid/IotMhDataBusiness/list",
                async:false,
                data: args,
                success: function (data) {
                    if(data.length && data.length>0){
                        exists=true;
                     }else{
                        exists=false;
                    }
                }
            });
	        if(exists){
	            $(element).data('error-msg','编码不能重复！');
	            return false;
	        }
        return true;
    }
}, function(params, element) {
    return $(element).data('error-msg');
});
/**/
